/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"AwesomeProject"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  [self performSelector:@selector(test) withObject:nil afterDelay:3];
  NSString *bundlePath = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"].absoluteString;
  NSLog(@"bundel path %@",bundlePath);
  NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
  NSLog(@"%@",path);
  return YES;
}

- (void)downloadBundle {
  NSString *url = @"https://qdct01.baidupcs.com/file/70e5b55a24a99c936fc54ccb9134a7b6?bkt=p3-140070e5b55a24a99c936fc54ccb9134a7b6d823ae930000000de6f3&fid=3545256676-250528-298357373863083&time=1558339287&sign=FDTAXGERLQBHSKfW-DCb740ccc5511e5e8fedcff06b081203-iY8HHdSleIUUYWXTl9x8aaQze4k%3D&to=90&size=911091&sta_dx=911091&sta_cs=0&sta_ft=jsbundle&sta_ct=0&sta_mt=0&fm2=MH%2CYangquan%2CAnywhere%2C%2Cguangdong%2Cct&ctime=1558339243&mtime=1558339243&resv0=cdnback&resv1=0&vuk=3545256676&iv=0&htype=&newver=1&newfm=1&secfm=1&flow_ver=3&pkey=140070e5b55a24a99c936fc54ccb9134a7b6d823ae930000000de6f3&sl=68616270&expires=8h&rt=sh&r=856078132&mlogid=3239287658539785415&vbdid=1424085397&fin=main.jsbundle&rtype=1&dp-logid=3239287658539785415&dp-callid=0.1.1&hps=1&tsl=200&csl=200&csign=hHxnoRCeOxilNRsYWo83Fm%2BBFAc%3D&so=0&ut=6&uter=4&serv=0&uc=879241619&ti=26fa64dbec2882246052f62ef68ce4877c7c453ea0efe1ae305a5e1275657320&by=themis";
  NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
  NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
  [data writeToFile:[path stringByAppendingPathComponent:@"main.jsbundle"] atomically:YES];
}

- (void)exchangeBundle {
  NSString *bundlePath = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"].path;
  NSLog(@"bundel path %@",bundlePath);
  NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
  NSLog(@"%@",path);
  
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error = nil;
  BOOL b = [fileManager removeItemAtPath:bundlePath error:&error];
  NSLog(@"if remove success %d %@",b,error);
  b = [fileManager moveItemAtPath:[path stringByAppendingPathComponent:@"main.jsbundle"] toPath:bundlePath error:nil];
  NSLog(@"if remove success %d",b);
}

- (void)test {
  UIButton *hotFixTest = [[UIButton alloc] initWithFrame:CGRectMake(50, 80, 60, 30)];
  hotFixTest.backgroundColor = [UIColor redColor];
  [hotFixTest addTarget:self action:@selector(exchangeBundle) forControlEvents:UIControlEventTouchUpInside];
  [hotFixTest setTitle:@"热更新" forState:UIControlStateNormal];
  [self.window addSubview:hotFixTest];
  
  UIButton *downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 80, 80, 30)];
  downloadButton.backgroundColor = [UIColor redColor];
  [downloadButton addTarget:self action:@selector(downloadBundle) forControlEvents:UIControlEventTouchUpInside];
  [downloadButton setTitle:@"下载bundle" forState:UIControlStateNormal];
  [self.window addSubview:downloadButton];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end
